layui.use([ 'table', 'form', 'layer', 'laydate', 'laytpl', 'element' ], function() {
	var table = layui.table, 
		form = layui.form, 
		layer = layui.layer, 
		$ = layui.jquery,
		laydate = layui.laydate, 
		laytpl = layui.laytpl,
		element = layui.element;

	/*加载信息类型表格*/
	table.render({
		elem : '#articleType',
		id:'articleTypeid',
		url : '../informationmanagement/getinfotype',
		toolbar : '#toolbarDemo',
		title : '信息类型数据表',
		height: "full-75",
		skin : 'line',
		even : true,
		cols : [ 
		     [ {
				type : 'numbers',
				title : '序号',
				align : 'center',
				width : 80
			}, {
				field : 'typename',
				align : 'center',
				title : '类型名称',
			}, {
				field : 'describe',
				align : 'center',
				title : '类型描述'
			},{
				field : 'isdelete',
				align : 'center',
				title : '是否启用',
				templet : '#powerbar',
			},{
				title : '操作',
				toolbar : '#barDemo',
				align : 'center'
			} ] 
		 ],
		 page: {
				layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
				groups: 5,
				limit: 10,
				limits: [10, 20, 30, 40, 50],
				theme: '#1E9FFF',						
		 },
	});
	
	//监听锁定操作
	form.on('checkbox(lockDemo)', function(obj) {
		if(obj.elem.checked){
			$.ajax({
				type : 'get',
				url : '../informationmanagement/enableinfotype?typeid=' + this.name,
				datatype : 'json',
				success : function(data) {
					if (data.code == "10001") {
						layer.alert(data.msg, {icon: 1}); 
					} else {
						layer.alert(data.msg, {icon: 2});
					}
				},
				error : function() {
					layer.alert('出现错误，请重试！', {icon: 2});
				}
			});
		}
		else{
			$.ajax({
				type : 'get',
				url : '../informationmanagement/disenableinfotype?typeid=' + this.name,
				datatype : 'json',
				success : function(data) {
					if (data.code == "10001") {
						layer.alert(data.msg, {icon: 1});
					} else {
						layer.alert(data.msg, {icon: 2});
					}
				},
				error : function() {
					layer.alert('出现错误，请重试！', {icon: 2});
				}
			});
		}						
	});

	//添加信息类型管理
	table.on('toolbar(articleType)', function(obj) {
		switch (obj.event) {
			case 'addartType':
				$("#typeName").val("");
				$("#description").val("");
				layer.open({
					type : 1,
					title : '信息类型添加',
					area : [ '460px', '310px' ],
					shade : 0.4,
					content : $('#add-artTypeLayer'),
					btn : [ '保存', '返回' ],
					yes : function() {
						var typeName = $("#typeName").val();
						var description = $("#description").val();
	
						if(typeName == "") {
							layer.tips('不能为空', '#typeName');
							return;
						} 
						else if(description == "") {
							layer.tips('不能为空', '#description');
							return;
						}	
						$.ajax({
							type : 'get',
							url : '../informationmanagement/addinfotype?typename=' + typeName + '&describe=' + description,
							datatype : 'json',
							success : function(data) {
								if (data.code == "10001") {
									layer.confirm(data.msg, {
									  btn: ['确定']
									}, function(){
										table.reload("articleTypeid", { //此处是上文提到的 初始化标识id
							                where: {
							                	keyword:data.code=='10001'
							                }
							            });	
										layer.closeAll();
									});
								}else{
									layer.confirm(data.msg, {
									  btn: ['确定']
									});
									layer.setTop(layero);
								}
							},
							error : function() {
								layer.confirm('出现错误，添加失败，请重试！', {
									  btn: ['确定']
								});
							}
						});						
					},
					btn2 : function() {
						layer.closeAll();
					},
				});
			break;
		};
	});

	//表格工具栏事件 
	table.on('tool(articleType)', function(obj) {
		var data = obj.data;
		switch (obj.event) {
			case 'edit':
				$("#typeName").val(data.typename);
				$("#description").val(data.describe);
				layer.open({
					type : 1,
					title : '轶事类型修改',
					area : [ '460px', '310px' ],
					shade : 0.4,
					content : $('#add-artTypeLayer'),
					btn : [ '保存', '返回' ],
					yes : function() {
						var typeid = data.typeid;
						var typeName = $("#typeName").val();
						var description = $("#description").val();
	
						if(typeName == "") {
							layer.tips('不能为空', '#typeName');
							return;
						} 
						else if(description == "") {
							layer.tips('不能为空', '#description');
							return;
						}	
						$.ajax({
							type : 'get',
							url : '../informationmanagement/updateinfotype?typename=' + typeName + '&describe=' + description + '&typeid=' + typeid,
							datatype : 'json',
							success : function(data) {
								if (data.code == "10001") {
									layer.confirm(data.msg, {
									  btn: ['确定']
									}, function(){
										table.reload("articleTypeid", { //此处是上文提到的 初始化标识id
							                where: {
							                	keyword:data.code=='10001'
							                }
							            });	
										layer.closeAll();
									});
								}else{
									layer.confirm(data.msg, {
									  btn: ['确定']
									});
									layer.setTop(layero);
								}
							},
							error : function() {
								layer.confirm('出现错误，修改失败，请重试！', {
									  btn: ['确定']
								});
							}
						});		
					},
					btn2 : function() {
						layer.closeAll();
					},
				});
			break;
			
			//删除按钮操作
			case 'del':
				//询问框
				layer.confirm('确定要删除么？', {
				  btn: ['确定','取消'] //按钮
				}, function(){
					$.ajax({
		        		type: 'get',
		        		url: "../informationmanagement/deleteinfotype?typeid=" + data.typeid,
		        		dataType: 'json',
		        		success:function(data){
		        			if(data.code == 10001){
		        				layer.confirm(data.msg, {
								  btn: ['确定']
								}, function(){
									table.reload("articleTypeid", { //此处是上文提到的 初始化标识id
						                where: {
						                	keyword:data.code=='10001'
						                }
						            });	
									layer.closeAll();
								});          				 
		        			}
		        			else{
		        				layer.confirm(data.msg, {
									  btn: ['确定']
								});
		        			}
		        		},
		        		error:function(){
		        			layer.confirm('出现错误，删除失败，请重试！', {
								  btn: ['确定']
							});
		        		},
		        	});   
				}, function(){ 
					layer.closeAll();
				});
			break;
			
		}
		;
	});

});